package com.ccx.coachsite.ui.manage;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ccx.coachsite.R;
import com.ccx.coachsite.bus.DateParams;
import com.ccx.coachsite.bus.RankInfo;
import com.ccx.coachsite.bus.SalaryInfo;
import com.ccx.coachsite.ui.own.OwerBaseView;
import com.ccx.coachsite.ui.own.OwerMainBase;


public class ManageSalaryView extends OwerBaseView{
	
	protected SalaryInfo mSalaryInfo;
	protected View mRootView;
	protected ImageView tmpView;
	protected int mSelectIndex;
	
	private List<DateParams> dateLists;
	
	public ManageSalaryView(Context context, OwerMainBase superView) {
		super(context, superView);
		initViews();
	}
	
	protected void initViews()
	{
		mRootView = findViewById(R.id.rootRankId);	
		loadData();
	}
	
	protected String getDateString(Calendar cal)
	{
		return cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH)+1) + "月" + cal.get(Calendar.DAY_OF_MONTH) + "日";
	}
	
	protected String getDateStringFormat(Calendar cal)
	{
		return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH)+1) + "-" + cal.get(Calendar.DAY_OF_MONTH);
	}
	
	protected String getDateTitleString(Calendar cal)
	{
		return cal.get(Calendar.YEAR) + "年" + (cal.get(Calendar.MONTH)+1) + "月";
	}
	
	protected void initDate()
	{
		dateLists = new ArrayList<DateParams>();
		
		Date curDate = new Date(System.currentTimeMillis());
		Calendar cal = Calendar.getInstance();
		cal.setTime(curDate);
		
		DateParams param1 = new DateParams();
		dateLists.add(param1);
		
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		String end1 = getDateString(cal);
		param1.setEndStr(getDateStringFormat(cal));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		String start1 = getDateString(cal);
		param1.setBeginStr(getDateStringFormat(cal));
		param1.setTitleStr(getDateTitleString(cal));
		
		DateParams param2 = new DateParams();
		dateLists.add(param2);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		String end2 = getDateString(cal);
		param2.setEndStr(getDateStringFormat(cal));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		String start2 = getDateString(cal);
		param2.setBeginStr(getDateStringFormat(cal));
		param2.setTitleStr(getDateTitleString(cal));
		
		DateParams param3 = new DateParams();
		dateLists.add(param3);
		
		cal.add(Calendar.DAY_OF_MONTH, -1);
		String end3 = getDateString(cal);
		param3.setEndStr(getDateStringFormat(cal));
		cal.set(Calendar.DAY_OF_MONTH, 1);
		String start3 = getDateString(cal);
		param3.setBeginStr(getDateStringFormat(cal));
		param3.setTitleStr(getDateTitleString(cal));
		
		TextView date1 = (TextView)mRootView.findViewById(R.id.ower_more_title_score1);
		date1.setText(start1 + "-" + end1);
		
		TextView date2 = (TextView)mRootView.findViewById(R.id.ower_more_title_score2);
		date2.setText(start2 + "-" + end2);
		
		TextView date3 = (TextView)mRootView.findViewById(R.id.ower_more_title_score3);
		date3.setText(start3 + "-" + end3);
		
		View scoreBtn1 = mRootView.findViewById(R.id.ower_more_btn_score1);
		scoreBtn1.setOnClickListener(new OnButtonListener());
		
		View scoreBtn2 = mRootView.findViewById(R.id.ower_more_btn_score2);
		scoreBtn2.setOnClickListener(new OnButtonListener());
		
		View scoreBtn3 = mRootView.findViewById(R.id.ower_more_btn_score3);
		scoreBtn3.setOnClickListener(new OnButtonListener());
		
		setImageViewById(R.id.ower_more_btn_score1);
		
		View btnQuery = mRootView.findViewById(R.id.manage_rank_salary_query);
		btnQuery.setOnClickListener(new OnQueryButtonListener());
	}
	
	class OnQueryButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) 
		{
			DateParams param = dateLists.get(mSelectIndex - 1);
			mSuperView.SwitchView(ManageKey.ll_manage_salary_query, param);
			//ManageMainView mainView = (ManageMainView)mSuperView;
			//mainView.SwitchSalaryQuery(ManageKey.ll_manage_salary_query, param);
		}
	}
	
	class OnButtonListener implements OnClickListener {
		@Override
		public void onClick(View v) 
		{
			setImageViewById(v.getId());
		}
	}
	
	private void setImageViewById(int id)
	{
		ImageView img = null;
		switch(id)
		{
			case R.id.ower_more_btn_score1:
				img = (ImageView)mRootView.findViewById(R.id.ower_more_img_score1);
				mSelectIndex = 1;
				break;
			case R.id.ower_more_btn_score2:
				img = (ImageView)mRootView.findViewById(R.id.ower_more_img_score2);
				mSelectIndex = 2;
				break;
			case R.id.ower_more_btn_score3:
				img = (ImageView)mRootView.findViewById(R.id.ower_more_img_score3);
				mSelectIndex = 3;
				break;
				default:
					break;
		}
		
		if(tmpView != null)
		{
			tmpView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_signal_unchecked));
		}
		
		if(img != null)
		{
			img.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ower_image_signal_checked));
			tmpView = img;
		}
	}
	
	protected boolean onLoadData() 
	{
		mSalaryInfo = ManageProvider.getInstance().getSalaryInfo(mContext);
		if (mSalaryInfo == null)
			return false;
		return true;
	}
	
	protected void doRefreshDataSuccess()
	{
		initDate();
		
		TextView txtSalary = (TextView)mRootView.findViewById(R.id.manage_salary_value);
		txtSalary.setText(mSalaryInfo.getSalary()+"");
		
		TextView txtRank = (TextView)mRootView.findViewById(R.id.manage_salary_rank_value);
		txtRank.setText(mSalaryInfo.getRank()+"");
		
		List<RankInfo> ranks = mSalaryInfo.getRankList();
		if(ranks !=null && ranks.size() >= 3)
		{
			RankInfo rinfo1 = ranks.get(0);
			TextView txtRankName1 = (TextView)mRootView.findViewById(R.id.manage_rank_name1);
			txtRankName1.setText(rinfo1.getName());
			
			TextView txtRankValue1 = (TextView)mRootView.findViewById(R.id.manage_rank_salary1);
			txtRankValue1.setText(rinfo1.getSalary()+"");
			
			RankInfo rinfo2 = ranks.get(1);
			TextView txtRankName2 = (TextView)mRootView.findViewById(R.id.manage_rank_name2);
			txtRankName2.setText(rinfo2.getName());
			
			TextView txtRankValue2 = (TextView)mRootView.findViewById(R.id.manage_rank_salary2);
			txtRankValue2.setText(rinfo2.getSalary()+"");
			
			RankInfo rinfo3 = ranks.get(2);
			TextView txtRankName3 = (TextView)mRootView.findViewById(R.id.manage_rank_name3);
			txtRankName3.setText(rinfo3.getName());
			
			TextView txtRankValue3 = (TextView)mRootView.findViewById(R.id.manage_rank_salary3);
			txtRankValue3.setText(rinfo3.getSalary()+"");
			
		}
	}
}
